﻿using CefSharp;
using CefSharp.WinForms;
using LearnCefsharp;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ThirdApp
{
    public class CefCustomObject
    {
        // Declare a local instance of chromium and the main form in order to execute things from here in the main thread
        private static ChromiumWebBrowser _instanceBrowser = null;
        // The form class needs to be changed according to yours
        private static FrmMain _instanceMainForm = null;


        public CefCustomObject(ChromiumWebBrowser originalBrowser, FrmMain mainForm)
        {
            _instanceBrowser = originalBrowser;
            _instanceMainForm = mainForm;
        }

        /// <summary>
        /// 打开调试工具
        /// </summary>
        public void showDevTools()
        {
            _instanceBrowser.ShowDevTools();
        }

        /// <summary>
        /// 打开命令行工具
        /// </summary>
        public void opencmd()
        {
            ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause");
            Process.Start(start);
        }

        /// <summary>
        /// 切换网页
        /// </summary>
        public void changePage()
        {
            _instanceBrowser.Load("www.163.com");
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        public void closeForm()
        {
            try
            {
                _instanceBrowser.CloseDevTools();
                _instanceBrowser.GetBrowser().CloseBrowser(true);
            }
            catch { }

            try
            {
                if (_instanceBrowser != null)
                {
                    _instanceBrowser.Dispose();
                }
            }
            catch { }

            if (!_instanceMainForm.IsDisposed && _instanceMainForm.IsHandleCreated)
            {
                var result = _instanceMainForm.BeginInvoke(new Action(() =>
                {

                    if (_instanceMainForm != null && !_instanceMainForm.IsDisposed)
                    {
                        _instanceMainForm.Close();
                        _instanceMainForm.Dispose();
                    }
                }));
                _instanceMainForm.EndInvoke(result);
            }

        }

        /// <summary>
        /// 查找电脑信息
        /// </summary>
        /// <param name="javascriptCallback"></param>
        public void FindComputerInfo(IJavascriptCallback javascriptCallback)
        {

            Task.Factory.StartNew(async () =>
            {

                using (javascriptCallback)
                {
                    Computer computer = new Computer();
                    string response = JsonConvert.SerializeObject(new
                    {
                        cpu_id = computer.CPU_Id,
                        disk_id = computer.Disk_Id,
                        host_name = computer.HostName,
                        networkcard = computer.NetworkCard,
                        serialNumber = computer.SerialNumber_Manufacturer_Product.Item1,
                        manufacturer = computer.SerialNumber_Manufacturer_Product.Item2,
                        product = computer.SerialNumber_Manufacturer_Product.Item3,
                    });
                    await javascriptCallback.ExecuteAsync(response);
                }
            });

        }

    }
}
